﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using Cosmetic.Logic;
using System.Collections.Generic;

namespace WebApp
{
    public partial class test : System.Web.UI.Page
    {
        private ProductManager m_ProductManager = new ProductManager();

        protected void Page_Load(object sender, EventArgs e)
        {
            List<Cosmetic.DataEntity.Product> products = m_ProductManager.GetAll();

            foreach (Cosmetic.DataEntity.Product item in products)
            {
                //Response.Write(item.ProductId);
                //Response.Write("<br/>");
                //item.UID = UUID();

                if (item.UID == null)
                {
                    item.UID = UUID();
                    m_ProductManager.Update();
                }
                else
                {
                    Response.Write(item.ProductId);
                    Response.Write("     ");
                    Response.Write(item.UID);

                    Response.Write("<br/>");
                }
            }
            //ReverseC("abcdf");
            //string s ="abcdf";//声明一个字符串
            //char[] ca=s.ToCharArray();//将此字符串复制到UNICODE字符串
            //Array .Reverse(ca);//用 Reverser（）方法反转数组
            //s = new string(ca);

        }

        public static string UUID()
        {
            byte[] buffer = Guid.NewGuid().ToByteArray();
            //byte[] buffer = Origin.ToByteArray();
            long long_guid = BitConverter.ToInt64(buffer, 0);

            string _Value = System.Math.Abs(long_guid).ToString();

            byte[] buf = new byte[_Value.Length];
            int p = 0;
            for (int i = 0; i < _Value.Length; )
            {
                byte ph = System.Convert.ToByte(_Value[i]);

                int fix = 1;
                if ((i + 1) < _Value.Length)
                {
                    byte pl = System.Convert.ToByte(_Value[i + 1]);
                    buf[p] = (byte)((ph << 4) + pl);
                    fix = 2;
                }
                else
                {
                    buf[p] = (byte)(ph);
                }

                if ((i + 3) < _Value.Length)
                {
                    if (System.Convert.ToInt16(_Value.Substring(i, 3)) < 256)
                    {
                        buf[p] = System.Convert.ToByte(_Value.Substring(i, 3));
                        fix = 3;
                    }
                }
                p++;
                i = i + fix;
            }
            byte[] buf2 = new byte[p];
            for (int i = 0; i < p; i++)
            {
                buf2[i] = buf[i];
            }
            string cRtn = System.Convert.ToBase64String(buf2);
            if (cRtn == null)
            {
                cRtn = "";
            }
            cRtn = cRtn.ToLower();
            cRtn = cRtn.Replace("/", "");
            cRtn = cRtn.Replace("+", "");
            cRtn = cRtn.Replace("=", "");
            if (cRtn.Length == 12)
            {
                return cRtn;
            }
            else
            {
                return UUID();
            }
        }

        public string ReverseC(string text)
        {
            //声明一个栈（先进后出）
            Stack resultStack = new Stack();
            //将UNICODE字符串载入到栈中
            foreach (char c in text)
            {
                resultStack.Push(c);
                
            }
            StringBuilder sb = new StringBuilder();
            while (resultStack.Count > 0)
            {
                sb.Append(resultStack.Pop());//弹出来
            }
            return sb.ToString();
        }

        public string ReverseB(string text)
        {
            char[] charArray = text.ToCharArray();
            Array.Reverse(charArray);
            return new string(charArray);
        }

       


    }
}
